package juc.safefinal;

/**
 * @ClassName Foo2
 * @PackageName juc.safefinal
 * @projectName myThread
 * @Description 
 * @Author games
 * @Date 2025/8/29 上午9:49
 * @Version 1.0
 */
import java.util.HashMap;
import java.util.Map;

public final class Foo2 {
    private final int age;
    private final String name;

    private static final Map<String, Foo2> cache = new HashMap<>();

    private Foo2(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public static Foo2 create(int age, String name) {
        String key = age + ":" + name;
        return cache.computeIfAbsent(key, k -> new Foo2(age, name));
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Foo2 Foo2 = (Foo2) o;
        return age == Foo2.age && name.equals(Foo2.name);
    }

    @Override
    public int hashCode() {
        return 31 * age + name.hashCode();
    }
}
